package com.servlettest.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servury2Servlet extends HttpServlet {


	private static final long serialVersionUID = 7084341770323053021L;

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	@SuppressWarnings("rawtypes")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		request.setCharacterEncoding("utf-8");
//		Enumeration e=request.getParameterNames();
//		String parameterName=null;
//		while(e.hasMoreElements())
//		{
//			parameterName=(String)e.nextElement();
//			String[] values=request.getParameterValues(parameterName);
//			out.println("参数名称："+parameterName);
//			out.println("<ul>");
//			for(int i=0;i<values.length;i++)
//			{
//				out.println("<li>"+values[i]+"</li>");
//			}
//			out.println("</ul><br>");
//		}
		Map map=request.getParameterMap();
		Set set=map.keySet();
		Iterator it = set.iterator();
		String parameterName=null;
		while(it.hasNext())
		{
			parameterName=(String)it.next();
			String[] values=request.getParameterValues(parameterName);
			out.println("参数名称："+parameterName);
			out.println("<ul>");
			for(int i=0;i<values.length;i++)
			{
				out.println("<li>"+values[i]+"</li>");
			}
			out.println("</ul><br>");
		}
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	public String fiterHtml(String value) {
		value = value.replaceAll("&", "&amp;");
		value = value.replaceAll("<", "&lt;");
		value = value.replaceAll(">", "&gt;");
		value = value.replaceAll(" ", "&nbsp;");
		value = value.replaceAll("'", "&#39");
		value = value.replaceAll("\"", "&quot;");
		value = value.replaceAll("\n", "<br>");

		return value;
	}
}
